package com.peng.test.fastjson;

import com.alibaba.fastjson2.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * SampleFastJson
 *
 * @author: lupeng6
 * @create: 2023/4/9 22:13
 */
@Slf4j
public class SampleFastJson {

    /**
     * string 转 json
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t1() {
        String json = """
                {
                    "name": "asdasfs",
                    "age": 1111
                }
                """;
        JSONObject jsonObject = JSON.parseObject(json);
        System.out.println(jsonObject.get("name"));
        System.out.println(jsonObject.get("age"));
    }

    /**
     * string 2 jsonArray
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t2() {
        String json = """
                [
                    {
                    "name": "asdasfs",
                    "age": 1111
                },
                {
                    "name": "asdasfs1",
                    "age": 11111
                }
                ]
                """;
        JSONArray jsonArray = JSON.parseArray(json);
        System.out.println(jsonArray.getJSONObject(0).get("name"));
        System.out.println(jsonArray.getJSONObject(1).get("age"));
    }

    /**
     * java obj 2 json
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t3() {
        Bean1 b = new Bean1();
        b.setName("zhangsan");
        b.setAge(123);
        System.out.println(JSON.toJSONString(b));
    }

    /**
     * 获取属性
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t4() {
        String json = """
                [111, 222, 333]
                """;
        JSONArray jsonArray = JSON.parseArray(json);
        System.out.println(jsonArray.getIntValue(1));

        // JSONObject 获取属性
        String json1 = """
                {
                    "flag": true,
                    "name": "lisi"
                }
                """;
        JSONObject jsonObject = JSON.parseObject(json1);
        System.out.println(jsonObject.getBooleanValue("flag", false));
    }

    /**
     * 读取java bean
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t5() {
        String json = """
                [
                    {
                    "name": "asdasfs",
                    "age": 1111
                },
                {
                    "name": "asdasfs1",
                    "age": 11111
                }
                ]
                """;
        JSONArray jsonArray = JSON.parseArray(json);
        Bean1 b = jsonArray.getObject(1, Bean1.class);
        System.out.println(b);
    }

    /**
     * jsonobj 2 javabean
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t6() {
        String json = """
                [
                    {
                    "name": "asdasfs",
                    "age": 1111
                },
                {
                    "name": "asdasfs1",
                    "age": 11111
                }
                ]
                """;
        JSONArray jsonArray = JSON.parseArray(json);
        List<Bean1> list = jsonArray.toJavaList(Bean1.class);
        System.out.println(list);
    }

    /**
     * jsonpath
     *
     * @author lupeng6
     * @date 2023/4/9 22:16
     */
    @Test
    public void t7() {
        String json = """
                [
                    {
                    "name": "asdasfs",
                    "age": 1111
                },
                {
                    "name": "asdasfs1",
                    "age": 222
                }
                ]
                """;
        JSONPath jsonPath = JSONPath.of("$[0]['age']");
        Object extract = jsonPath.extract(JSONReader.of(json));
        System.out.println(extract);
    }

    @Data
    static class Bean1 {
        private String name;
        private int age;
    }

    @Test
    public void t8() {
        try {
            throw new RuntimeException("asdfsafd");
        } catch (Exception e) {
            log.error("error", e);
        }
    }

}
